
var gtc;

function trSetup() {
	tc = new Object();
	tc['tc_num'] = 123;
	tc['tc_name'] = "250000 Postmark";
	tc['tc_summary'] = "Create 250000 files using Postmark"
	tc['tc_desc'] = "";
	steps = new Array();
	tc['tc_steps'] = steps;
	runs = new Array();
	tc['tc_runs'] = runs;
	run = new Object();
	run['tr_num'] = 3;
	run['tr_date'] = "28-05-09"
	run['tr_who'] = "shaun.savage"
	run['tr_kbuild'] = 68
	run['tr_pbuild'] = 68
	run['tr_cluster'] = "bb37s7-8"
	run['tr_quick'] = "proto 2"
	run['tr_notes'] = "did 70293 files in one directory before fail"
	run['tr_bug'] = "28640"
	run['tr_status'] = "FAIL"
	run['tr_status_color'] = "red"
	//runs.splice(0,0,run);
	//fillTC( tc);
}

function clearRun () {
    document.getElementById("tc_name").value = "";
    document.getElementById("tc_desc").value = "";
	document.getElementById("tr_status_deck").selectedIndex = 0;
	document.getElementById("tr_status_show").setAttribute("label", "");
	document.getElementById("tr_date").value = "";
	document.getElementById("tr_tester").value = "";
	document.getElementById("tr_kbuild").value = "";
	document.getElementById("tr_pbuild").value = "";
	document.getElementById("tr_cluster").value = "";
	document.getElementById("tr_quick").value = "";
	document.getElementById("tr_notes").value = "";
	document.getElementById("tr_bug").value = "";
	cleanHistory();
}


function fillRun( run) {
	var readonly = true;
	var tr_status_deck = document.getElementById("tr_status_deck");
	var tr_status_show = document.getElementById("tr_status_show");
	var tr_status_edit = document.getElementById("tr_status_edit");
	var tr_date = document.getElementById("tr_date");
	var tr_who = document.getElementById("tr_tester");
	var tr_kbuild = document.getElementById("tr_kbuild");
	var tr_pbuild = document.getElementById("tr_pbuild");
	var tr_cluster = document.getElementById("tr_cluster");
	var tr_quick = document.getElementById("tr_quick");
	var tr_notes = document.getElementById("tr_notes");
	var tr_bug = document.getElementById("tr_bug");
	var tr_history_deck = document.getElementById("tr_history_deck");
	if (run == null) {
		readonly = false;
		tr_status_deck.setAttribute("selectedIndex", 1);
		tr_history_deck.setAttribute("selectedIndex", 1);
		run = new Object();
		run['tr_date'] = "";
		run['tr_tester'] = "";
		run['tr_kbuild'] = "";
		run['tr_pbuild'] = "";
		run['tr_cluster'] = "";
		run['tr_quick'] = "";
		run['tr_notes'] = "";
		run['tr_bug'] = "";
		tr_status_edit.setAttribute("selectedIndex", 0);
		tr_status_edit.setAttribute("style", "color:green");
    	document.getElementById("tr_num").value = "";
	} else {
    	document.getElementById("tr_num").value = run['tr_num'] ;
		tr_status_deck.setAttribute("selectedIndex", 0);
		tr_history_deck.setAttribute("selectedIndex", 0);
		tr_status_show.setAttribute("label", run['tr_status']);
		tr_status_show.setAttribute("style", "color:"+getStatusColor(run['tr_status']));
	}
	tr_date.readOnly = readonly;
	tr_date.value = run['tr_date'];
	tr_who.readOnly = readonly;
	tr_who.value = run['tr_tester'];
	tr_kbuild.readOnly = readonly;
	tr_kbuild.value = run['tr_kbuild'];
	tr_pbuild.readOnly = readonly;
	tr_pbuild.value = run['tr_pbuild'];
	tr_cluster.readOnly = readonly;
	tr_cluster.value = run['tr_cluster'];
	tr_quick.readOnly = readonly;
	tr_quick.value = run['tr_quick'];
	tr_notes.readOnly = readonly;
	tr_notes.value = run['tr_notes'];
	tr_bug.readOnly = readonly;
	tr_bug.value = run['tr_bug'];
}

function fillTCrun( tc) {
	gtc = tc;
    var tc_name = document.getElementById("tc_name");
    var tc_desc = document.getElementById("tc_desc");
    document.getElementById("tr_testcase").value = tc['tc_num'] ;
    document.getElementById("tr_num").value = "" ;
	tc_name.value = tc['tc_name'];
	tc_desc.value = tc['tc_summary'];
	var runs = tc['tc_runs'];
	cleanHistory();
	if (runs.length > 0) {
		fillRun( runs[0]);
		for each ( run in runs) {
			fillHistory( run);
		}
	} else {
		fillRun( null)
	}	
}

function cleanHistory() {
    var tr_history_children = document.getElementById("tr_history_children");
	while ( ele = tr_history_children.firstChild) {
		tr_history_children.removeChild(ele);
	}
}

function fillHistory( run) {
    var tr_history_children = document.getElementById("tr_history_children");
	var row = document.createElement("treerow");
	var cell = document.createElement("treecell");
	cell.setAttribute("label", run['tr_status']);
	cell.setAttribute("properties", getStatusColor(run['tr_status']));
	cell.setAttribute("value", run['tr_num']);
	row.appendChild(cell);
	cell = document.createElement("treecell");
	cell.setAttribute("label", run['tr_date']);
	row.appendChild(cell);
	cell = document.createElement("treecell");
	cell.setAttribute("label", run['tr_notes']);
	row.appendChild(cell);

	cell = document.createElement("treecell");
	cell.setAttribute("label", run['tr_tester']);
	row.appendChild(cell);
	cell = document.createElement("treecell");
	var build = run['tc_kbuild'] + "/" + run['tc_pbuild'];
	cell.setAttribute("label", build);
	row.appendChild(cell);
	cell = document.createElement("treecell");
	cell.setAttribute("label", run['tr_cluster']);
	row.appendChild(cell);
	cell = document.createElement("treecell");
	cell.setAttribute("label", run['tr_bug']);
	row.appendChild(cell);
	cell = document.createElement("treecell");
	cell.setAttribute("label", run['tr_dump']);
	row.appendChild(cell);

	var item = document.createElement("treeitem");
	item.appendChild(row);
	tr_history_children.appendChild(item);
}

function newRun() {
	if (document.getElementById('tr_testcase').value == "")
		return;
	fillRun(null);
	var d = new Date();
	d = d.toLocaleDateString();
	document.getElementById("tr_date").value = d;
}

function selectStatus() {
	var tr_status_edit = document.getElementById("tr_status_edit");
	var color = tr_status_edit.selectedItem.getAttribute("value");
	tr_status_edit.setAttribute("style","color:"+color);
}

function addRun() {
	var tr_status_edit = document.getElementById("tr_status_edit");
	var tr_date = document.getElementById("tr_date");
	var tr_tester = document.getElementById("tr_tester");
	var tr_kbuild = document.getElementById("tr_kbuild");
	var tr_pbuild = document.getElementById("tr_pbuild");
	var tr_cluster = document.getElementById("tr_cluster");
	var tr_quick = document.getElementById("tr_quick");
	var tr_notes = document.getElementById("tr_notes");
	var tr_bug = document.getElementById("tr_bug");
	var item = tr_status_edit.selectedItem;
	run = new Object();
	run['tr_status'] = item.getAttribute("label");
	run['tr_date'] = tr_date.value;
	run['tr_tester'] = tr_tester.value;
	run['tr_kbuild'] = tr_kbuild.value;
	run['tr_pbuild'] = tr_pbuild.value;
	run['tr_cluster'] = tr_cluster.value;
	run['tr_quick'] = tr_quick.value;
	run['tr_notes'] = tr_notes.value;
	run['tr_bug'] = tr_bug.value;
	run['tr_dump'] = "";
	run['tr_testcase'] = document.getElementById("tr_testcase").value; 
	run['tr_num'] = document.getElementById("tr_num").value; 
	cmd = {};
    cmd['type'] = "cmd";
    cmd['cmd'] = "newTR";
    cmd['run'] = run;
    listtc_conn = new tcListener( JSON.stringify(cmd));
	
	document.getElementById('tr_history_deck').setAttribute("selectedIndex", 0);
}

function cancelRun() {
	fillRun(null);
	document.getElementById("tr_status_deck").setAttribute("selectedIndex", 0);
	document.getElementById("tr_history_deck").setAttribute("selectedIndex", 0);
}

function deleteRun() {
}

